﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Dandan
{
    public partial class MainForm1 : Form
    {
        public MainForm1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            GetFieldsData();
            //InitHeader();
            GetOrderData();
            GetCommoditysData();
            //GetModelsData();
        }
        
        OrderData orderData;
        public void GetOrderData()
        {
            string Url = "/api/v1/orders";

            string json = HttpWebResponseUtility.MakeRequestGet(Url);
            try
            {
                orderData = JsonConvert.DeserializeObject<OrderData>(json);
                //OrderData.SetData(orderData);
                this.BeginInvoke((MethodInvoker)delegate ()
                {

                    OrderlistView1.View = View.Details;
                    ColumnHeader clh;
                    clh = new ColumnHeader();
                    clh.Text = "订单编号";
                    clh.Width = 200;
                    OrderlistView1.Columns.Add(clh);

                    clh = new ColumnHeader();
                    clh.Text = "商品编号";
                    clh.Width = 200;
                    OrderlistView1.Columns.Add(clh);

                    clh = new ColumnHeader();
                    clh.Text = "商品名字";
                    clh.Width = 200;
                    OrderlistView1.Columns.Add(clh);

                    clh = new ColumnHeader();
                    clh.Text = "出售数量";
                    clh.Width = 200;
                    OrderlistView1.Columns.Add(clh);

                    clh = new ColumnHeader();
                    clh.Text = "单件价格";
                    clh.Width = 200;
                    OrderlistView1.Columns.Add(clh);
                    //以上代码为添加列

                    //OrderSN.SubItems.Add("1111111111111");
                    //OrderSN.SubItems.Add("2222222222222");
                    for (int i = 0; i < orderData.data.Length; i++)
                    {
                        ListViewItem OrderSN = new ListViewItem(orderData.data[i].sn);      //11是String型，为第1列内容
                        OrderSN.SubItems.Add(orderData.data[i].projects[0].sn);
                        OrderSN.SubItems.Add(orderData.data[i].projects[0].title);
                        OrderSN.SubItems.Add(orderData.data[i].projects[0].count);
                        OrderSN.SubItems.Add(orderData.data[i].projects[0].price);
                        OrderlistView1.Items.Add(OrderSN);
                    }
                    Test();
                });
            }
            catch (Exception)
            {
                MessageBox.Show("系统提示" + json);
            }
        }

        public void GetFieldsData()
        {
            string Url = "/api/v1/models/" + 1 + "/fields";

            string json = HttpWebResponseUtility.MakeRequestGet(Url);
            try
            {
                FieldsData.setInstance(JsonConvert.DeserializeObject<FieldsData>(json));
                //OrderData.SetData(orderData);
                this.BeginInvoke((MethodInvoker)delegate ()
                {

                });
            }
            catch (Exception)
            {

            }
        }
        
        public void GetCommoditysData()
        {
            String Url = "/api/v1/projects?" + UserData.GetData().data.shops[0].id + "per_page=10&page=" + 1;

            string json = HttpWebResponseUtility.MakeRequestGet(Url);
            try
            {
                ModelsData.SetData(JsonConvert.DeserializeObject<ModelsData>(json));
                //OrderData.SetData(orderData);
                this.BeginInvoke((MethodInvoker)delegate ()
                {
                    listViewExtended1.Columns.Clear();
                    listViewExtended1.Groups.Clear();
                    listViewExtended1.Items.Clear();

                    ColumnHeader clh = new ColumnHeader();
                    clh.Text = "品牌";
                    clh.Width = 80;

                    ColumnHeader clh1 = new ColumnHeader();
                    clh1 = new ColumnHeader();
                    clh1.Text = "名字";
                    clh1.Width = 80;

                    ColumnHeader clh2 = new ColumnHeader();
                    clh2 = new ColumnHeader();
                    clh2.Text = "颜色";
                    clh2.Width = 100;

                    ColumnHeader clh3 = new ColumnHeader();
                    clh3 = new ColumnHeader();
                    clh3.Text = "尺寸";
                    clh3.Width = 100;

                    ColumnHeader clh4 = new ColumnHeader();
                    clh4 = new ColumnHeader();
                    clh4.Text = "进价";
                    clh4.Width = 100;

                    ColumnHeader clh5 = new ColumnHeader();
                    clh5 = new ColumnHeader();
                    clh5.Text = "售价";
                    clh5.Width = 100;

                    ColumnHeader clh6 = new ColumnHeader();
                    clh6 = new ColumnHeader();
                    clh6.Text = "折扣";
                    clh6.Width = 100;

                    ColumnHeader clh7 = new ColumnHeader();
                    clh7 = new ColumnHeader();
                    clh7.Text = "库存";
                    clh7.Width = 100;

                    ColumnHeader clh8 = new ColumnHeader();
                    clh8 = new ColumnHeader();
                    clh8.Text = "商品类别";
                    clh8.Width = 0;


                    ColumnHeader clh9 = new ColumnHeader();
                    clh9 = new ColumnHeader();
                    clh9.Text = "组别";
                    clh9.Width = 0;
                    
                    listViewExtended1.Columns.AddRange(new ColumnHeader[] { clh, clh1, clh2, clh3, clh4, clh5, clh6, clh7 , clh8 , clh9 });
                    

                    for (int i = 0; i < ModelsData.GetData().data.Length; i++)
                    {
                        ListViewGroup group1 = new ListViewGroup(ModelsData.GetData().data[i].brand.title + "       " + ModelsData.GetData().data[i].title);

                        listViewExtended1.Groups.Add(group1);

                        ModelsData aa = ModelsData.GetData();
                        // Create items and add them to myListView.  
                        for (int a = 0; a < ModelsData.GetData().data[i].groups.Count; a++)
                        {
                            //MessageBox.Show(ModelsData.GetData().data[i].groups[a].shopinfo[0].base_price);
                            if (aa.data[i].groups[a].fields.Count > 1)
                            {
                                /*string t1 = aa.data[i].groups[a].fields[0].value;
                                string t2 = aa.data[i].groups[a].fields[1].value;
                                string t3 = aa.data[i].groups[a].shopinfo[0].base_price;
                                string t4 = aa.data[i].groups[a].shopinfo[0].sale_price;
                                string t5 = aa.data[i].groups[a].shopinfo[0].discount_value;
                                string t6 = aa.data[i].groups[a].shopinfo[0].stock;*/

                                ListViewItem item = new ListViewItem(new string[] { " ", " ",
                                    aa.data[i].groups[a].fields[0].value,
                                    aa.data[i].groups[a].fields[1].value,
                                    aa.data[i].groups[a].shopinfo[0].base_price ,
                                    aa.data[i].groups[a].shopinfo[0].sale_price,
                                    aa.data[i].groups[a].shopinfo[0].discount_value,
                                    aa.data[i].groups[a].shopinfo[0].stock,
                                    i.ToString(),
                                    a.ToString()
                                }, 0, group1);
                                listViewExtended1.Items.Add(item);
                            }
                            else
                            {
                                ListViewItem item = new ListViewItem(new string[] { " ", " ",
                                    "",
                                    "",
                                    aa.data[i].groups[a].shopinfo[0].base_price ,
                                    aa.data[i].groups[a].shopinfo[0].sale_price,
                                    aa.data[i].groups[a].shopinfo[0].discount_value,
                                    aa.data[i].groups[a].shopinfo[0].stock,
                                    i.ToString(),
                                    a.ToString()
                                }, 0, group1);
                                listViewExtended1.Items.Add(item);
                            }
                            /*ListViewItem item = new ListViewItem(new string[] { " ", " ",
                                aa.data[i].groups[a].fields[0].value,
                                aa.data[i].groups[a].fields[1].value,
                                aa.data[i].groups[a].shopinfo[0].base_price ,
                                aa.data[i].groups[a].shopinfo[0].sale_price,
                                aa.data[i].groups[a].shopinfo[0].discount_value,
                                aa.data[i].groups[a].shopinfo[0].stock,
                                i.ToString(),
                                a.ToString()
                            }, 0, group1);*/
                            //listViewExtended1.Items.Add(item);
                        }
                    }

                    //listViewExtended1.Items.AddRange(new ListViewItem[] { item0, item1, item2, item3, item4, item5 });
                });
            }
            catch (Exception ex)
            {
                MessageBox.Show("系统提示" + ex);
            }
        }

        public void RefreshCommoditysData()
        {
            listViewExtended1.Groups.Clear();
            listViewExtended1.Items.Clear();


            for (int i = 0; i < ModelsData.GetData().data.Length; i++)
            {
                ListViewGroup group1 = new ListViewGroup(ModelsData.GetData().data[i].brand.title + "       " + ModelsData.GetData().data[i].title);

                listViewExtended1.Groups.Add(group1);

                // Create items and add them to myListView.  
                for (int a = 0; a < ModelsData.GetData().data[i].groups.Count; a++)
                {

                    ListViewItem item = new ListViewItem(new string[] { " ", " ", ModelsData.GetData().data[i].groups[a].fields[0].value, ModelsData.GetData().data[i].groups[a].fields[1].value,
                                ModelsData.GetData().data[i].groups[a].shopinfo[0].base_price ,
                                ModelsData.GetData().data[i].groups[a].shopinfo[0].sale_price,
                                ModelsData.GetData().data[i].groups[a].shopinfo[0].discount_value,
                                ModelsData.GetData().data[i].groups[a].shopinfo[0].stock,
                                i.ToString(),
                                a.ToString()
                            }, 0, group1);
                    listViewExtended1.Items.Add(item);
                }
            }
        }

        private void MainForm1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("确定要退出吗?", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                e.Cancel = false;       //关闭窗体
            }
            else
            {
                e.Cancel = true;                  //不执行操作
            }
        }

        public void Test()
        {
            fieldsData m_fieldsData = new fieldsData();
            New_ModelsData modelsData = new New_ModelsData();
            for (int i = 0; i < FieldsData.getInstance().data.Count; i++)
            {
                m_fieldsData = new fieldsData();
                String title = FieldsData.getInstance().data[i].title;
                m_fieldsData.title = title;
                m_fieldsData.validate_rule = FieldsData.getInstance().data[i].validate_rule;
                m_fieldsData.id = 1;
                m_fieldsData.name = FieldsData.getInstance().data[i].name;
                
                m_fieldsData.id = FieldsData.getInstance().data[i].id;
                modelsData.group.fields.Add(m_fieldsData);
            }
            modelsData.baseInfo.brand = "花花公子";
            modelsData.baseInfo.title = "短衫";
            modelsData.group.fields[0].value = "黑色";
            modelsData.group.fields[1].value = "大";
            modelsData.group.shop.base_price = "1";
            modelsData.group.shop.sale_price = "2";
            modelsData.group.shop.discount_value = "100";
            modelsData.group.shop.stock = "5";
            string strJson = JsonConvert.SerializeObject(modelsData);


            ListViewItem m_ListViewItem = new ListViewItem(modelsData.baseInfo.brand);
            m_ListViewItem.SubItems.Add(modelsData.baseInfo.title);
            m_ListViewItem.SubItems.Add(modelsData.group.fields[0].value);
            m_ListViewItem.SubItems.Add(modelsData.group.fields[1].value);
            m_ListViewItem.SubItems.Add(modelsData.group.shop.base_price);
            m_ListViewItem.SubItems.Add(modelsData.group.shop.sale_price);
            m_ListViewItem.SubItems.Add(modelsData.group.shop.discount_value);
            m_ListViewItem.SubItems.Add(modelsData.group.shop.stock);
            listViewExtended1.Items.Add(m_ListViewItem);
        }

        private void MainForm1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }
        
        private void listViewExtended1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            int a = listViewExtended1.FocusedItem.Index;//获取当前选择项索引
            New_AddPreinstallCommodity frm = new New_AddPreinstallCommodity();
            frm.MainForm = this;
            InboundModelsData m_ModelsData = new InboundModelsData();
            int ModelsId = Convert.ToInt32(this.listViewExtended1.Items[a].SubItems[this.listViewExtended1.Items[a].SubItems.Count - 2].Text);
            int GroupId = Convert.ToInt32(this.listViewExtended1.Items[a].SubItems[this.listViewExtended1.Items[a].SubItems.Count - 1].Text);
            m_ModelsData.baseinfo.brand_id = ModelsData.GetData().data[ModelsId].brand.id+"";
            m_ModelsData.baseinfo.sn = ModelsData.GetData().data[ModelsId].sn;
            m_ModelsData.baseinfo.title = ModelsData.GetData().data[ModelsId].title;
            m_ModelsData.baseinfo.brand = ModelsData.GetData().data[ModelsId].brand;

            m_ModelsData.group = ModelsData.GetData().data[ModelsId].groups[GroupId];

            m_ModelsData.id = ModelsData.GetData().data[ModelsId].id;
            m_ModelsData.shop_id = ModelsData.GetData().data[ModelsId].groups[GroupId].shopinfo[0].shop_id;

            frm.m_ModelsData = m_ModelsData;

            frm.ShowDialog();
            //MessageBox.Show(this.listViewExtended1.Items[a].Group.Header);
            //for (int i = 0; i < this.listViewExtended1.Items[a].SubItems.Count; i++)
            //{
            //MessageBox.Show(this.listViewExtended1.Items[a].SubItems[i].Text);
            //frm.str[i] = this.listViewExtended1.Items[a].SubItems[i].Text;
            //}
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            New_AddPreinstallCommodity frm = new New_AddPreinstallCommodity();
            frm.MainForm = this;
            for (int i = 0; i < FieldsData.getInstance().data.Count; i++)
            {
                FieldsData m_FieldsData = FieldsData.getInstance();
                fieldsData m_fieldsData = new fieldsData();
                m_fieldsData.name = m_FieldsData.data[i].name;
                m_fieldsData.title = m_FieldsData.data[i].title;

                frm.m_ModelsData.group.fields.Add(m_fieldsData);
            }
            frm.m_ModelsData.baseinfo.brand_id = "11";
            frm.ShowDialog();
        }
    }
}
